package com.zcz.umengpush.controller;

import com.alibaba.fastjson.JSON;
import com.zcz.umengpush.pojo.PushMsg;
import com.zcz.umengpush.pojo.PushUser;
import com.zcz.umengpush.pojo.Receipt;
import com.zcz.umengpush.service.Demo;
import com.zcz.umengpush.service.PushMsgService;
import com.zcz.umengpush.service.PushUserService;
import com.zcz.umengpush.util.PageResult;
import com.zcz.umengpush.util.Util;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

//import com.zcz.common.pojo.PageResult;

/**
 * @author 王卫帅
 */
@RestController
@Api(value = "友盟推送接口文档")
@RequestMapping("uMengPush")
public class UMengPushCtl{
    Logger logger = LoggerFactory.getLogger(getClass());
    //ios
    @Value("${umeng4j.appkeyIos}")
    private String appkeyIos;
    @Value("${umeng4j.appMasterSecretIos}")
    private String appMasterSecretIos;
    //Android
    @Value("${umeng4j.appkeyAndroid}")
    private String appkeyAndroid;
    @Value("${umeng4j.appMasterSecretAndroid}")
    private String appMasterSecretAndroid;
    @Value("${umeng4j.zczMiActivity}")
    private String zczMiActivity;
//    @Autowired
//    RedisUtil redisUtil;
    @Autowired
    private PushUserService pushUserService;
    @Autowired
    private PushMsgService pushMsgService;
    @GetMapping("/1")
    public  ResponseEntity< String> ss(){
        System.err.println("++++++++++++++++");
        String a = "aaaa";

       // pushMsgService.callProcedure();
        logger.info("访问成功"+a);
        return ResponseEntity.ok(a);
    }
   // @GetMapping("/2")
    public  ResponseEntity< List<PushMsg>> aa(){
        List<PushMsg> pushMsgs = pushMsgService.selecExchangeList();
        return ResponseEntity.ok(pushMsgs);
    }
   // @PostMapping("/3")
    public  ResponseEntity<PageResult<PushUser>> selectList(@ApiParam(value = "每页显示的数量", name="everyPage") @RequestParam("everyPage")Integer everyPage,
                                                            @ApiParam(value = "当前页", name="currentPage") @RequestParam("currentPage")Integer currentPage){
       System.err.println("++++++++++++++++++");
        PageResult<PushUser> pushUserPageResult = pushUserService.selectList(everyPage, currentPage);
        //System.err.println(pushUsers.size());
        System.err.println("++++++++++++++++");
        return ResponseEntity.ok(pushUserPageResult);
    }

    /**
     *  Android  iOS  单播 或者 列播
     * @return
     */
   // @ApiOperation(value = "Android  iOS  单播 或者 列播")
    //@GetMapping("/sendUnicast")
    public  ResponseEntity<Void> sendUnicast(@ApiParam(value = "播放的参数", name="userid")@RequestParam("userId") String userId,
                                                     @ApiParam(value = "推送的模板", name ="template")@RequestParam("template") String template){

        //根据app传过来的条件进行查询数据库表
        //遍历每条数据判断是 0 Android  还是  1 iOS
        if ("DeviceType".equals(0)){
            Demo demo = new Demo(appkeyAndroid, appMasterSecretAndroid);
            try {
               // demo.sendAndroidUnicast("获取的tokenid");
            } catch (Exception ex) {
                ex.printStackTrace();
            }

        }else {
            Demo demo = new Demo(appkeyIos, appMasterSecretIos);
            try {
                //demo.sendIOSUnicast("获取的tokenid");
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return ResponseEntity.status(HttpStatus.OK).build();
    }
    /**
     * Android  ios 广播发送
     * @return
     */
    @ApiOperation(value = "Android  ios 广播发送")
    @PostMapping("/sendBroadcast")
    @ResponseBody
    public  ResponseEntity<String> sendBroadcast(@ApiParam(value = "推送的模板", name ="template") String template) {
        String s = "微服务调用成功"+template;
        System.err.println("*************微服务调用成功"+template);
        /* Demo demo = new Demo(appkeyAndroid, appMasterSecretAndroid);
        try {
            //安卓广播
            //demo.sendAndroidBroadcast();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        //查询消息推送用户注册的数据表
        List<String> list = new ArrayList<>();
        list.add("4dcc5de58916d55ac4390a9790bff60c5bd5e882d5004881e040416dde1d00c2");
        list.add("60762b3d75d9032672c6f683d7cca33d43c47c762b51d1f60f4edb99fa2d62dc");
        list.add("cd25893aff76899357228784b4ab1977bc755a701bf76001488e9b9761e2bc89");
        //遍历每条数据判断是 1 iOS
        for (String string : list) {
            //if ("DeviceType".equals(1)) {
            if (true) {
                Demo demoios = new Demo(appkeyIos, appMasterSecretIos);
                try {
                    //由于iOS需要加角标  广播 也会变成单播
                  //  demoios.sendIOSUnicast(string);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }

            }
        }*/

        return ResponseEntity.ok(s);

    }

    /**
     * ios 把角标值改为0
     * @return
     */
    //@ApiOperation(value = "ios 把角标值改为0")
    //@GetMapping("clearBadge")
    public ResponseEntity<Void> clearBadge(@ApiParam(value = "用户的id",name = "userid") String userid){
//        Jedis jedis = redisUtil.getJedis();
//        jedis.set("umeng"+userid,"0");
        return  ResponseEntity.status(HttpStatus.OK).build();
    }
    /**
     * 推送消息回执  如果使用 数据库表需要添加一个字段 消息的唯一标识   msg_id字段
     * @return
     */
    //@ResponseBody
    //@ApiOperation(value = "消息推送回执")
    //@PostMapping("pushReceipt")
    public void pushReceipt(HttpServletRequest request) throws IOException {
        byte buffer[] = Util.getRequestPostBytes(request);
        String charEncoding = request.getCharacterEncoding();
        if(charEncoding == null){
            charEncoding = "UTF-8";
        }
        String s = new String(buffer, charEncoding);
        List<Receipt> list = JSON.parseArray(s, Receipt.class);
        for (Receipt receipt : list) {
            //调用service的方法 进行业务逻辑操作
            System.err.println(receipt);
        }
        System.err.println(s);
       // return s;
    }


}
